﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYCodeBase.WinForm;

namespace ElectricDim
{
	public partial class DlgWireNumDim : DlgModelLess
	{
		public DlgWireNumDim(UIApplication uiApp, WindowHandle handel) : base(handel)
		{
			this.mApp = uiApp;
			this.InitializeComponent();
		}

		public static DlgWireNumDim GetInstance(UIApplication app, WindowHandle handel)
		{
			if (DlgWireNumDim.dll == null || DlgWireNumDim.dll.IsDisposed)
			{
				DlgWireNumDim.dll = new DlgWireNumDim(app, handel);
			}
			return DlgWireNumDim.dll;
		}

		public int Num
		{
			get
			{
				int result;
				try
				{
					result = Convert.ToInt32(this.txtNum.Text);
				}
				catch
				{
					this.txtNum.Text = "2";
					result = 2;
				}
				return result;
			}
			set
			{
				this.txtNum.Text = value.ToString();
			}
		}

		public Autodesk.Revit.DB.ElementType GetElementType(double angle)
		{
			if (this.Num >= 2)
			{
				Autodesk.Revit.DB.ElementType result = null;
				Family family = null;
				if (this.Num == 2)
				{
					family = ElectricMark.WireNumMarkOfTwo(this.mApp.ActiveUIDocument.Document)[0].LoadInSubTrans(this.mApp.ActiveUIDocument.Document);
				}
				else if (this.Num == 3)
				{
					family = ElectricMark.WireNumMarkOfThree(this.mApp.ActiveUIDocument.Document)[0].LoadInSubTrans(this.mApp.ActiveUIDocument.Document);
				}
				else if (this.Num > 3)
				{
					family = ElectricMark.WireNumMarkOfMore(this.mApp.ActiveUIDocument.Document)[0].LoadInSubTrans(this.mApp.ActiveUIDocument.Document);
				}
				string b;
				if ((angle > 0.0 && angle <= 15.0) || (angle > 180.0 && angle <= 195.0))
				{
					b = "15°";
				}
				else if ((angle > 15.0 && angle <= 30.0) || (angle > 195.0 && angle <= 210.0))
				{
					b = "30°";
				}
				else if ((angle > 30.0 && angle <= 45.0) || (angle > 210.0 && angle <= 225.0))
				{
					b = "45°";
				}
				else if ((angle > 45.0 && angle <= 60.0) || (angle > 225.0 && angle <= 240.0))
				{
					b = "60°";
				}
				else if ((angle > 60.0 && angle <= 75.0) || (angle > 240.0 && angle <= 255.0))
				{
					b = "75°";
				}
				else if ((angle > 75.0 && angle <= 90.0) || (angle > 255.0 && angle <= 270.0))
				{
					b = "90°";
				}
				else if ((angle > 90.0 && angle <= 105.0) || (angle > 270.0 && angle <= 285.0))
				{
					b = "-75°";
				}
				else if ((angle > 105.0 && angle <= 120.0) || (angle > 285.0 && angle <= 300.0))
				{
					b = "-60°";
				}
				else if ((angle > 120.0 && angle <= 135.0) || (angle > 300.0 && angle <= 315.0))
				{
					b = "-45°";
				}
				else if ((angle > 135.0 && angle <= 150.0) || (angle > 315.0 && angle <= 330.0))
				{
					b = "-30°";
				}
				else if ((angle > 165.0 && angle <= 180.0) || (angle > 345.0 && angle <= 360.0))
				{
					b = "0°";
				}
				else
				{
					b = "0°";
				}
				if (family != null)
				{
					foreach (Autodesk.Revit.DB.ElementType elementType in family.GetSymbols())
					{
						if (elementType.Name == b)
						{
							result = elementType;
							break;
						}
					}
				}
				return result;
			}
			throw new Exception("请填写大于1的根数。");
		}

		private void txtNum_MouseLeave(object sender, EventArgs e)
		{
			if (this.Num < 2)
			{
				MessageBox.Show("请填写大于1的根数。");
				this.txtNum.Text = "2";
				this.Num = 2;
			}
		}

		private UIApplication mApp;

		private static DlgWireNumDim dll;
	}
}
